﻿using Common;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace CodeCreator
{
    public partial class FrmDbManager : Form
    {

        string dbConfigFilePath = System.Windows.Forms.Application.StartupPath + "/Config/dbinfo.xml";

        public FrmDbManager()
        {
            InitializeComponent();
            List<DbInfo> dbInfoList = ToolsSerialize.XmlDeserialize<List<DbInfo>>(dbConfigFilePath);
            if (dbInfoList != null)
            {
                foreach (DbInfo item in dbInfoList)
                {
                    this.lstDbInfo.Items.Add(item);
                }
            }
        }

        private void lstDbInfo_SelectedIndexChanged(object sender, EventArgs e)
        {
            DbInfo item = this.lstDbInfo.SelectedItem as DbInfo;
            if (item != null)
            {
                this.txtName.Text = item.Name;
                this.txtConnectionString.Text = item.ConnectionString;
                this.cmbDbType.SelectedItem = item.Type;
            }

        }

        private void FrmDbManager_FormClosed(object sender, FormClosedEventArgs e)
        {
            List<DbInfo> dbInfoList = new List<DbInfo>();
            for (int i = 0; i < this.lstDbInfo.Items.Count; i++)
            {
                DbInfo item = this.lstDbInfo.Items[i] as DbInfo;
                if (item != null)
                {
                    dbInfoList.Add(item);
                }
            }
            ToolsSerialize.XmlSerialize<List<DbInfo>>(dbInfoList,dbConfigFilePath);
            this.DialogResult = System.Windows.Forms.DialogResult.OK;
        }

        private void button_Click(object sender, EventArgs e)
        {
            Button btn = sender as Button;
            if (btn != null)
            {
                switch (btn.Text)
                {
                    case "添加":
                        if (!hasSameName(this.txtName.Text,""))
                        {
                            if (this.txtName.Text.Trim() == "")
                            {
                                MessageBox.Show("名称不能为空！");
                                return;
                            }
                            if (this.txtConnectionString.Text.Trim() == "")
                            {
                                MessageBox.Show("数据库连接不能为空！");
                                return;
                            }

                            DbInfo newItem = new DbInfo();
                            newItem.ConnectionString = this.txtConnectionString.Text;
                            newItem.Name = this.txtName.Text;
                            newItem.Type = this.cmbDbType.SelectedItem.ToString();
                            this.lstDbInfo.Items.Add(newItem);
                        }
                        else
                        {
                            MessageBox.Show("存在该名称，请更换！");
                        }
                        break;
                    case "修改":
                        DbInfo item = this.lstDbInfo.SelectedItem as DbInfo;
                        if (item != null)
                        {
                            if (this.txtName.Text.Trim() == "")
                            {
                                MessageBox.Show("名称不能为空！");
                                return;
                            }
                            if (this.txtConnectionString.Text.Trim() == "")
                            {
                                MessageBox.Show("数据库连接不能为空！");
                                return;
                            }
                            if (!hasSameName(this.txtName.Text,item.Name))
                            {
                                item.ConnectionString = this.txtConnectionString.Text;
                                item.Name = this.txtName.Text;
                                item.Type = this.cmbDbType.SelectedItem.ToString();
                                refresh();
                            }
                            else
                            {
                                MessageBox.Show("存在该名称，请更换！");
                            }
                        }
                        break;
                    case "删除":
                        if (this.lstDbInfo.SelectedIndex >= 0)
                        {
                            this.lstDbInfo.Items.RemoveAt(this.lstDbInfo.SelectedIndex);
                        }
                        break;
                    case "测试":
                        break;
                }
            }

        }

        private bool hasSameName(string strName,string exceptName)
        {
            bool result = false;
            for (int i = 0; i < this.lstDbInfo.Items.Count; i++)
            {
                DbInfo item = this.lstDbInfo.Items[i] as DbInfo;
                if (item != null)
                {
                    if (item.Name.Trim() == strName.Trim())
                    {
                        if (item.Name != exceptName)
                        {
                            result = true;
                            break;
                        }
                    }
                }
            }
            return result;
        }

        private void refresh()
        {
            List<DbInfo> allDbInfo = new List<DbInfo>();
            for (int i = 0; i < this.lstDbInfo.Items.Count; i++)
            {
                DbInfo item = this.lstDbInfo.Items[i] as DbInfo;
                if (item != null)
                {
                    allDbInfo.Add(item);
                }
            }
            this.lstDbInfo.Items.Clear();
            foreach (DbInfo item in allDbInfo)
            {
                this.lstDbInfo.Items.Add(item);
            }
        }

        private void FrmDbManager_Load(object sender, EventArgs e)
        {

        }
    }
}
